package com.foreveross.fetl.web.action.preprocess;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.foreveross.fetl.common.HBaseTable;
import com.foreveross.fetl.common.hbase.client.HBaseHelper;
import com.foreveross.fetl.common.vo.model.Agent;
import com.foreveross.fetl.common.vo.model.SinglePlaneInfo;
import com.foreveross.fetl.util.DateUtils;
import com.foreveross.fetl.util.HbaseToPlaneInfoUtil;
import com.foreveross.fetl.util.RowkeyUtils;
import com.foreveross.fetl.web.action.BaseAction;

@Controller("agentCountAction")
@Scope("prototype")
public class AgentCountAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -148102743077319876L;
	
	public String list() {
		return "METHOD";
	}
	
	public String agentCount(){
		String date =getRequest().getParameter("date");
		String flightDate=getRequest().getParameter("flightDate");
		if(null!=flightDate){
			flightDate=flightDate.replace("-", "");
		}
		Map<String,Map<String,List<String>>> map =new HashMap<String,Map<String,List<String>>>();
		String fromCity=getRequest().getParameter("fromCity");
		String toCity=getRequest().getParameter("toCity");
		Date startTime = DateUtils.parse(date, "yyyy-MM-dd");
		List<String> channels=this.getCompareChannels();
		Calendar cEnd = Calendar.getInstance();
		cEnd.setTime(startTime);
		cEnd.add(Calendar.DAY_OF_MONTH, 1);
		for(String channel:channels){
			Calendar cStart = Calendar.getInstance();
			cStart.setTime(startTime);
			while (cStart.before(cEnd)) {
				String startRowKey =RowkeyUtils.getRowKey(DateUtils.format(cStart.getTime(), "yyyyMMddHH"), channel, fromCity, toCity,flightDate,RowkeyUtils.START_ROW_KEY);
				String endRowKey=RowkeyUtils.getRowKey(DateUtils.format(cStart.getTime(), "yyyyMMddHH"), channel, fromCity, toCity,flightDate, RowkeyUtils.STOP_ROW_KEY);
				ResultScanner rs = null;
				try {
					rs = HBaseHelper.getRecords(HBaseTable.BASE_DATA_FLIGHT,
							startRowKey, endRowKey,new String[]{"planeinfo","agentInfos"},null);
					for (Result res : rs) {
						SinglePlaneInfo vo =null;
						Map<String, Map<String, String>> curMap = HBaseHelper
								.transformHBaseRowToMap(res);
						vo =HbaseToPlaneInfoUtil.hbaseToSinglePlaneInfo(curMap, 1,SinglePlaneInfo.class);
						if(null==vo || null==vo.getAgents())
							continue;
						String carrierKey= vo.getCarrierKey();
						if(null==carrierKey){
							carrierKey=vo.getFlightNo().substring(0, 2);
						}
						this.checkAgent(map, channel, carrierKey, vo.getAgents());
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}finally{
					if(rs != null) rs.close();
					rs = null;
				}
				cStart.add(Calendar.HOUR_OF_DAY, 1);// 开始时间加一小时
			}
			
		}
		Map<String,Map<String,Integer>> count=new HashMap<String,Map<String,Integer>>(); 
		for(String channel:map.keySet()){
			Map<String,Integer> carrierKeyMap=new HashMap<String,Integer>();
			for(String ca:map.get(channel).keySet()){
				carrierKeyMap.put(ca, map.get(channel).get(ca).size());
			}
			count.put(channel, carrierKeyMap);
		}
		dataMap.put("count",count);
		return "JSON";
	}
	
	/**
	 * 渠道
	 * @return
	 */
	private List<String> getCompareChannels(){
		List<String> chanels=new ArrayList<String>();
		chanels.add("00001"); //淘宝
		chanels.add("00002");//去哪儿
		return chanels;
	}
	
	private void checkAgent(Map<String,Map<String,List<String>>> map,String channel,String carrierKey,List<Agent> listAgent ){
		Map<String,List<String>> keyMap=null;
		if(null==map.get(channel)){
			keyMap=new HashMap<String,List<String>>();
			keyMap.put(carrierKey, this.getListAgent(listAgent));
			map.put(channel, keyMap);
		}else{
			keyMap=map.get(channel);
			if(null==keyMap.get(carrierKey)){
				keyMap.put(carrierKey, this.getListAgent(listAgent));
			}else{
				List<String> agents =this.getListAgent(listAgent);
				for(String agentName:agents ){
					if(keyMap.get(carrierKey).indexOf(agentName)<0){
						keyMap.get(carrierKey).add(agentName);
					}
				}
			}
		}
	}
	
	private List<String> getListAgent(List<Agent> listAgent){
		List<String> list =new ArrayList<String>();
		if(null!=listAgent && listAgent.size()>0){
			for(Agent vo :listAgent){
				list.add(vo.getName());
			}
		}
		return list;
	}
 
}
